package org.dizem.groupon.action;

import com.opensymphony.xwork2.Preparable;
import org.dizem.groupon.model.domain.CategoryEntity;
import org.dizem.groupon.service.CategoryService;
import org.dizem.util.Pager;

import java.util.List;

/**
 * User: dizem@126.com
 * Time: 11-6-28 下午1:44
 */
public class CategoryAction extends BaseAction implements Preparable {
	private CategoryService service;
	private List<CategoryEntity> categories;
	private int id;
	private CategoryEntity category;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public CategoryEntity getCategory() {
		return category;
	}

	public void setCategory(CategoryEntity category) {
		this.category = category;
	}

	public CategoryService getService() {
		return service;
	}

	public void setService(CategoryService categoryService) {
		this.service = categoryService;
	}

	public List<CategoryEntity> getCategories() {
		return categories;
	}

	public void setCategories(List<CategoryEntity> categories) {
		this.categories = categories;
	}

	public void prepare() throws Exception {
		categories = service.loadAll();
	}

	public String listCategory() throws Exception {
		if (this.getRecordCount() < 0) {
			setRecordCount(service.getCount());
		}
		if (getPage() == null) {
			setPage(1);
		}
		Pager p = new Pager(getPage(), getRecordCount(), getPageSize());
		setPageCount(p.getPageCount());
		setPageUrl("listCategory");
		setPage(p.getPage());
		categories = service.load(p.getStartPage(), this.getPageSize());
		return SUCCESS;
	}

	public String delete() throws Exception {
		CategoryEntity category = service.getById(id);
		if (category != null)
			service.delete(category);
		return SUCCESS;
	}

	public String edit() throws Exception {
		category = service.getById(id);
		return SUCCESS;
	}

	public String add() throws Exception {
		service.save(category);
		return SUCCESS;
	}

	public String update() throws Exception {
		service.update(category);
		return SUCCESS;
	}
}
